package xjtu.se.articlecommunity.buser.articlemanage.impl;

import com.vdurmont.emoji.EmojiParser;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xjtu.se.articlecommunity.buser.articlemanage.ArticleManageService;
import xjtu.se.articlecommunity.buser.articlemanage.dao.ArticleManageDao;
import xjtu.se.articlecommunity.buser.articlemanage.dto.articleDto;
import xjtu.se.articlecommunity.buser.articlemanage.dto.at_tabDto;
import xjtu.se.articlecommunity.common.Utils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
@Transactional
public class ArticleManageServiceImpl implements ArticleManageService {
    @Resource
    private ArticleManageDao articleManageDao;

    @Override
    public Map<String, Object> getArticleList(articleDto dto) {
        Map<String,Object> map = Utils.getPage(dto, ()->articleManageDao.getArticleList(dto));
        List<articleDto> list = (List<articleDto>) map.get("list");
        for(articleDto art : list){
            String newStr = EmojiParser.parseToUnicode(art.getA_html());
            art.setA_html(newStr);
        }
        map.put("list",list);
        return map;
    }

    @Override
    public boolean isValidArticle(String a_id) {
        if (a_id == null){
            return false;
        }
        return articleManageDao.has_article(a_id) > 0;
    }

    @Override
    public boolean isValidTabs(List<String> tabIds) {
        for (String tab :
                tabIds) {
            if (!isValidTab(tab)) {
                return false;
            }
        }
        return true;
    }

    private boolean isValidTab(String at_id) {
        return articleManageDao.has_article_tab(at_id) > 0;
    }

    @Override
    public void setArticleTabs(String a_id, List<String> tabIds) {
        articleManageDao.removeRelatedFeed(a_id);
        articleManageDao.removeTabForArticle(a_id);
        for (String tab :
                tabIds) {
            articleManageDao.addTabToArticle(a_id, tab);
        }
    }

    @Override
    public List<at_tabDto> queryTabList(String a_id) {
        return articleManageDao.queryTabList(a_id);
    }

    @Override
    public void setArticleStatus(articleDto dto) {
        articleManageDao.setArticleStatus(dto.getA_id(), dto.getA_status());
    }

    @Override
    public void deleteTabByID(String at_id) {
        articleManageDao.deleteFeedWithTab(at_id);
        articleManageDao.deleteRelativeArtTabForID(at_id);
        articleManageDao.deleteTab(at_id);
    }

    @Override
    public int findTabByName(String at_name) {
        return articleManageDao.findTabByName(at_name);
    }

    @Override
    public String generateTab(String at_name) {
        String at_id;
        do{
            at_id = UUID.randomUUID().toString().replace("-", "");
        }while (isValidTab(at_id));

        articleManageDao.newTab(at_id, at_name);
        return at_id;
    }

    @Override
    public List<at_tabDto> queryALLTabs() {
        return articleManageDao.queryAllTabs();
    }
}
